package com.per.mybootall.webSocket;

import com.per.mybootall.vo.Message;
import com.per.mybootall.vo.WebSocketMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;

import javax.websocket.OnClose;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

//import static com.sun.deploy.si.SingleInstanceManager.broadcast;


@ServerEndpoint("/echo")
@Component
public class EchoSocket {
//    @Autowired
//    private SimpMessagingTemplate template;

private static String username;
private static List<String> names=new ArrayList<String>();
private static List<Session> sessions=new ArrayList<Session>();
    @OnOpen
    public void open(Session session) {
        //一个session代表一个即时通信会话
        String qrName=session.getQueryString();
        System.out.println("qrName:"+qrName);
       username=qrName.split("=")[1];
       names.add(username);
String msg="<span style=\"color: #CF1900;\">欢迎"+username+"用户进入</span><br/>";
sessions.add(session);
        Message message=new Message();
        message.setMsg(msg);
        message.setList(names);
        for(Session session1:sessions){
            try {
                session1.getBasicRemote().sendText(msg);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    //broadcast(message.toJSON());
    }

    @OnClose
    public void close(Session session){
        String qrname=session.getQueryString();
        username=qrname.split("=")[1];
        String msg="<span style=\"color: #CF1900;\">用户"+username+"已经退出登陆</span>";
        System.out.println("关闭"+session.getId()+"管道");
        sessions.remove(session);
        System.out.println("sessions  remove  session  success");
    for(Session session1:sessions){
        try {
            session1.getBasicRemote().sendText(msg);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    }

    @OnMessage
    public void message(Session session,String msg){
        System.out.println("客户端:"+msg);
        try {
           // session.getBasicRemote().sendText(msg);
            WebSocketMessage webSocketMessage=new WebSocketMessage();
            webSocketMessage.setMessage(msg);
            //  广播出去
            for(Session session1:sessions){
                if(session==session1){
                    String msgs="<span style=\"color: chartreuse;\">"+msg+"</span>";
                    session1.getBasicRemote().sendText(msgs);
                }else{
                    String msgs="名字后面改哈："+msg;
                    session1.getBasicRemote().sendText(msgs);
                }

            }


        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
